
#include <bitset>
#include <limits>
#include <limits.h>
#include <stdlib.h>
//#include <iostream>
#include <sstream>
using namespace std;

#include "HardDisk.h"
//#include "FileSys.h"

void printMenu() {
	cout << endl << "Menu:" << endl;
	cout << "1:  allocate inode" << endl;
	cout << "2:  free inode" << endl;
	cout << "3:  allocate block" << endl;
	cout << "4:  free block" << endl;
	cout << "5:  getInodeType" << endl;
	cout << "6:  setInodeType" << endl;
	cout << "7:  getDataBlock" << endl;
	cout << "8:  setDataBlock" << endl;
	cout << "9:  readBlock" << endl;
	cout << "10: writeBlock" << endl;
	cout << "11: getFileSize" << endl;
	cout << "12: setFileSize" << endl;

	cout << "0: exit" << endl;
}

int str2int(string s){
	int temp;
	stringstream s2(s);
	s2 >> temp;
	return temp;
}

void freeInode(HardDisk hd){
	string inode ;
	cout << "enter inode num:" << endl;
	getline(cin, inode);
	hd.freeDataBlock(str2int(inode));
	cout << "inode " << inode << " freed" << endl;
}

void freeBlock(HardDisk hd){
	string inode ;
	cout << "enter block num:" << endl;
	getline(cin, inode);

	hd.freeInode(str2int(inode));
	cout << "block " << inode << " freed" << endl;
}



void f5(HardDisk hd){
	string i_node;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	hd.getInodeType( str2int(i_node));
}
void f6(HardDisk hd){
	string i_node;
	string filetype;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	cout << "enter file type:" << endl;
	getline(cin, filetype);
	hd.setInodeType(str2int( i_node), str2int( filetype));
}
void f7(HardDisk hd){
	string i_node;
	string i;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	cout << "enter the i data block num:" << endl;
	getline(cin, i);
	hd.getDataBlock( str2int(i_node),  str2int(i));
}
void f8(HardDisk hd){
	string i_node;
	string i;
	string dblockNum;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	cout << "enter the i's data num:" << endl;
	getline(cin, i);
	cout << "enter the data block num:" << endl;
	getline(cin, i);
	hd.setDataBlock(str2int( i_node), str2int( i), str2int( dblockNum));
}
/*void f9(HardDisk hd){
	int dblockNum;
	string buf;
	hd.readBlock( dblockNum, buf.c_str());
}
void f10(HardDisk hd){
	int dblockNum;
	string newdata;
	hd.writeBlock( dblockNum,newdata.c_str());
}*/
void f11(HardDisk hd){
	string i_node;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	hd.getFileSize( str2int(i_node));
}
void f12(HardDisk hd){
	string i_node;
	string newSize;
	cout << "enter inode num:" << endl;
	getline(cin, i_node);
	cout << "enter the new size:" << endl;
	getline(cin, newSize);
	hd.setFileSize( str2int(i_node), str2int(newSize));
}
int main(int argc, char** argv) {

	HardDisk hd("config");
	//FileSys fSys("config");


	int userChoice = 0;
	string inode;

	do {
		printMenu();
		cin >> userChoice;
		getchar();

		cout << "userChoice is: " << userChoice << endl;
		switch (userChoice) {
		case 1:

			inode = hd.allocateInode();
			cout << "inode " << inode << " alocated" << endl;
			break;
		case 2:
			freeInode(hd);
			break;
		case 3:
			inode = hd.allocateDataBlock();
			cout << "block " << inode << " alocated" << endl;
			break;
		case 4:
			freeBlock(hd);
			break;
		case 5:
			f5(hd);
			break;
		case 6:
			f6(hd);
			break;
		case 7:
			f7(hd);
			break;
		case 8:
			f8(hd);
			break;
/*		case 9:
			f9(hd);
			break;
		case 10:
			f10(hd);
			break;*/
		case 11:
			f11(hd);
			break;
		case 12:
			f12(hd);
			break;

		}
	} while (userChoice != 0);
	cout << "end" << endl;
	return 0;
}
